#include "app_uart.h"
#include "iot_errno.h"
#include "stdint.h"
#include "iot_uart.h"
#include "app_log.h"

const static char *TAG = "app_uart.c";

#define UART_BUFF_SIZE 1000
#define WIFI_IOT_UART_IDX_1 1
#define TASK_DELAY_1S 1000000
static const char *data = "Hello, World!\r\n";

/**
 * @brief uart_test_task
 *
 */
void uart_test_task(void)
{
    uint8_t uart_buff[UART_BUFF_SIZE] = { 0 };
    uint8_t *uart_buff_ptr = uart_buff;
    uint8_t ret;

    IotUartAttribute uart_attr = {

        // baud_rate: 9600
        .baudRate = 9600,

        // data_bits: 8bits
        .dataBits = 8,
        .stopBits = 1,  /* 停止位 */
        .parity = 0,    /* 校验位 */
    };

    // Initialize uart driver
    ret = IoTUartInit(WIFI_IOT_UART_IDX_1, &uart_attr);
    if (ret != IOT_SUCCESS) 
    {
        MY_LOGI(TAG,"Failed to init uart! Err code = %d\n", ret);
        return;
    }

    while (1) 
    {
        // send data through uart1
        IoTUartWrite(WIFI_IOT_UART_IDX_1, (unsigned char *)data, strlen(data));

        // receive data through uart1
        IoTUartRead(WIFI_IOT_UART_IDX_1, uart_buff_ptr, UART_BUFF_SIZE);

        MY_LOGI(TAG,"Uart1 read data:%s\r\n", uart_buff_ptr);
        usleep(TASK_DELAY_1S);
    }
}
